
#include <BImage>
#include <BHtmlItem>
#include <BHtmlWidget>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BGridLayout>

#include "ImgInitBoard.h"

ImgInitBoard::ImgInitBoard(BWidget* parent) : Board(parent)
{
	this->setSize(500, 500);
	this->setTitle("Init img");

	_fileDialog = new BFileDialog(this);
	_fileDialog->setTitle("Open Image File");
	_fileDialog->setNameFilter("*.png,*.jpg,*.tif,*.bmp,*");

	BGridLayout* layout0 = new BGridLayout();
	{
		BLabel* filePathLabel = new BLabel("file path");
		_filePathEdit = new BLineEdit();
		_browseButton = new BButton(" ... ");
		BLabel* localPathLabel = new BLabel("local path");
		_localPathEdit = new BLineEdit();
		_localPathEdit->setReadOnly(true);
		layout0->addWidget(0, 0, filePathLabel);
		layout0->addWidget(0, 1, _filePathEdit);
		layout0->addWidget(0, 2, _browseButton);
		layout0->addWidget(1, 0, localPathLabel);
		layout0->addWidget(1, 1, _localPathEdit);
		layout0->setColumnStretch(0, 1);
		layout0->setColumnStretch(1, 10);
		layout0->setColumnStretch(2, 1);

	}

	_imageLabel = new BLabel();
	_imageLabel->setName("image");
	_imageLabel->setAlign(Align_Center);

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(layout0, 0);
	vlayout->addWidget(_imageLabel);

	BHBoxLayout* bottomLayout = new BHBoxLayout(this, Part_Bottom);
	{
		_confirmButton = new BButton("Confirm");
		_cancelButton = new BButton("Cancel");
		bottomLayout->addSpring();
		bottomLayout->addWidget(_confirmButton);
		bottomLayout->addWidget(_cancelButton);
	}

	connect(this, Signal_AssetChanged, &ImgInitBoard::slotAssetChanged);

	connect(_browseButton, Signal_Clicked, &ImgInitBoard::slotBrowseButtonClicked);
	connect(_confirmButton, Signal_Clicked, &ImgInitBoard::slotConfirmButtonClicked);
	connect(_cancelButton, Signal_Clicked, &ImgInitBoard::slotCancelButtonClicked);
}
ImgInitBoard::~ImgInitBoard()
{

}

void ImgInitBoard::slotAssetChanged(const BObject* object, const BValue& value)
{
	BHtmlItem* item = value;
	BString path = item->htmlWidget()->fileName().path();
	_fileDialog->setPath(path);
	BString fileName = item->attrib("src");
	if (fileName.size())
	{
		_localPathEdit->setText(fileName);
		int backCount = 0;
		while (fileName.beginWith("../"))
		{
			fileName.remove("../");
			backCount++;
		}
		while (backCount > 0)
		{
			int pos = path.rfind('/');
			path.truncate(pos);
			backCount--;
		}
		fileName.prepend('/');
		fileName.prepend(path);
		_filePathEdit->setText(fileName);
		BImage* image = new BImage(fileName);
		_imageLabel->setImage(image);
	}
	else
	{
		_localPathEdit->setText(BString());
		_filePathEdit->setText(BString());
		_imageLabel->setImage(0);
	}
}
void ImgInitBoard::slotBrowseButtonClicked(const BObject* object, const BValue& value)
{
	if (_fileDialog->execute())
	{
		BString fileName = _fileDialog->fileName();
		_filePathEdit->setText(fileName);
		BImage* image = new BImage(fileName);
		_imageLabel->setImage(image);
		BHtmlItem* item = this->asset();
		if (BHtmlWidget* htmlWidget = item->htmlWidget())
		{
			BString path = htmlWidget->fileName().path();
			int pos = 0;
			while (pos < path.size() && pos < fileName.size())
			{
				if (path[pos] != fileName[pos])
					break;
				pos++;
			}
			path.remove(0, pos);
			fileName.remove(0, pos);
			if (pos > 0)
			{
				if (path.size())
				{
					int level = path.count('/') + 1;
					for (int i = 0; i < level; i++)
					{
						fileName.prepend("../");
					}
				}
				else
				{
					fileName.removeHead('/');
				}
			}
		}
		item->setAttrib("src", fileName);
		_localPathEdit->setText(fileName);
	}
}
void ImgInitBoard::slotConfirmButtonClicked(const BObject* object, const BValue& value)
{
	this->close(true);
}
void ImgInitBoard::slotCancelButtonClicked(const BObject* object, const BValue& value)
{
	this->close();
}
